import sys
import numpy as np

def verify_result(output_file, golden_file):
    # 读取输出和 golden，dtype = uint8
    output = np.fromfile(output_file, dtype=np.uint8)
    golden = np.fromfile(golden_file, dtype=np.uint8)

    # 比较是否一致
    different_indexes = np.where(output != golden)[0]

    # 打印前 10 个不一致的元素
    for i, idx in enumerate(different_indexes):
        print(f"data index: {idx:06d}, expected: {golden[idx]}, actual: {output[idx]}")
        if i >= 9:
            break

    error_ratio = float(len(different_indexes)) / golden.size
    print(f"error ratio: {error_ratio:.4f}")

    return error_ratio == 0.0  # 输出必须完全一致

if __name__ == "__main__":
    try:
        if len(sys.argv) != 3:
            raise ValueError("Usage: python verify_result.py <output_file> <golden_file>")

        res = verify_result(sys.argv[1], sys.argv[2])
        if not res:
            raise ValueError("[ERROR] result mismatch")
        else:
            print("test pass")
    except Exception as e:
        print(e)
        sys.exit(1)
